﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using Dycox.ComponentModel;
using Dycox.Validation;

namespace Dycox.Workflows
{
    [DataContract]
    public class StageStateContract : StateContract
    {
        internal const string OperatorNameKey = "OperatorName", CommentKey = "Comment", UserActionKey = "$ACTION";

        public StageStateContract()
        {

        }

        public StageStateContract(StateObject obj)
            : base(obj)
        {

        }

        public void Reflect(IReflectionRoot target, WFStep step, bool modifying)
        {
            ReflectProperties(target, step, modifying);
        }

        protected virtual void ReflectProperties(object target, WFStep step, bool modifying)
        {
            ReflectProperties(target, ModelPropertyUsage.User, ModelPropertyUsage.Application);
        }

        protected void ReflectProperties(object target, params ModelPropertyUsage[] usages)
        {
            if (target != null)
            {
                PropertyDescriptorCollection pdct = TypeDescriptor.GetProperties(target);

                foreach (ModelPropertyUsage u in usages)
                {
                    PropertyDescriptorCollection pdcs = TypeDescriptor.GetProperties(this, new Attribute[] { ModelPropertyAttribute.GetInstance(u) });
                    foreach (PropertyDescriptor pds in pdcs)
                    {
                        PropertyDescriptor pdt = pdct[pds.Name];
                        if (pdt != null)
                            pdt.SetValue(target, pds.GetValue(this));
                    }

                }
            }
        }

        internal protected virtual void InitializeNew(OperationArguments args, Stage stage)
        {
        }

        [
        RequiredValidator(true, ErrorMessage = "必须提供经办人姓名"),
        DisplayName("经办人"),
        DefaultValue(null),
        DataMember(Name = OperatorNameKey, EmitDefaultValue = false)
        ]
        public string OperatorName
        {
            get { return this.GetValue<string>(OperatorNameKey); }
            set { this.SetValue(OperatorNameKey, value); }
        }

        [
        DisplayName("备注"),
        DefaultValue(null),
        DataMember(Name = CommentKey, EmitDefaultValue = false),
        RequiredValidator(true, Condition = "IsCommentRequired == true", ErrorMessage = "必须提供备注")
        ]
        public string Comment
        {
            get { return this.GetValue<string>(CommentKey); }
            set { this.SetValue(CommentKey, value); }
        }

        public virtual bool IsCommentRequired
        {
            get { return false; }
        }

        [DataMember(Name = UserActionKey, EmitDefaultValue = false)]
        public string UserAction
        {
            get { return this.GetValue<string>(UserActionKey); }
            set { this.SetValue(UserActionKey, value); }
        }

    }
}
